package com.jf.mall.domain;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-10-02
 * Time:3:50
 */
public class ShoppingCart {
    /*
    1.商品集合
    2.商品id哈希集
    3.商品数量总和
    4.商品总价
     */
    private Integer userId;
    private HashSet<Integer> ids = new HashSet<>();//Ids here are furniture's ids
    private List<Commodity> commodities = new ArrayList<>();
    private Map<Integer, Commodity> commodityMap = new HashMap<>();//The key in map is furniture's id

    //private Integer totalCount;
    //private BigDecimal totalPrice;
    public ShoppingCart() {
    }

    public ShoppingCart(Integer userId) {
        this.userId = userId;
    }

    public void addCommodity(int id, Furniture furniture) {
        if (furniture != null) {
            if (!ids.contains(id)) {
                ids.add(id);
                Commodity commodity = new Commodity(userId, furniture);
                commodities.add(commodity);
                commodityMap.put(id, commodity);
            } else {
                Commodity commodity = commodityMap.get(id);
                commodity.add();
                int index = commodities.indexOf(commodity);
                for (int i = index - 1; i >= 0; i--) {
                    commodities.set(i + 1, commodities.get(i));
                }
                commodities.set(0, commodity);
            }

        }
    }

    public void removeCommodity(int id) {
        Commodity commodity = commodityMap.get(id);
        if (commodity != null) {
            commodities.remove(commodity);
            commodityMap.remove(id);
            ids.remove(id);
        }
    }

    public void updateCommodityCount(int id, int count) {
        Commodity commodity = commodityMap.get(id);
        if (commodity != null) {
            commodity.updateCount(count);
        }
    }

    public Integer getUserId() {
        return userId;
    }

    public Integer[] getIds() {
        return ids.toArray(new Integer[0]);
    }

    public List<Commodity> getCommodities() {
        return commodities;
    }

    public Integer getTotalCount() {
        int totalCount = 0;
        for (Commodity commodity : commodities) {
            totalCount += commodity.getCount();
        }
        return totalCount;
    }

    public BigDecimal getTotalPrice() {
        BigDecimal totalPrice = new BigDecimal(0);
        for (Commodity commodity : commodities) {
            totalPrice = commodity.getTotalPrice().add(totalPrice);
        }
        return totalPrice;
    }

    public void updateCart(Map<Integer, Furniture> furnitureMap) {
        for (Map.Entry<Integer, Furniture> entry : furnitureMap.entrySet()) {
            int id = entry.getKey();
            Furniture furniture = entry.getValue();
            if (furniture == null) {
                removeCommodity(id);
            } else {
                updateCommodity(id, furniture);
            }
        }
    }

    public void updateCart(List<Commodity> commodities) {
        this.commodities = commodities;
        ids = new HashSet<>();
        commodityMap = new HashMap<>();
        for (Commodity commodity : commodities) {
            ids.add(commodity.getFurnitureId());
            commodityMap.put(commodity.getFurnitureId(), commodity);
        }
    }

    private void updateCommodity(int id, Furniture furniture) {
        Commodity commodity = commodityMap.get(id);
        if (commodity != null) {
            commodity.update(furniture);
        }
    }

    public boolean contains(int id) {
        return ids.contains(id);
    }

    public Commodity getCommodity(int id) {
        return commodityMap.get(id);
    }

    public boolean isEmpty() {
        return commodities.isEmpty();
    }
}
